package com.bailan.controller;

import com.bailan.pojo.Admin;
import com.bailan.pojo.DAO.PeopleManagerDAO;
import com.bailan.pojo.Student;
import com.bailan.service.IAdminService;
import com.bailan.service.IStudentService;
import com.bailan.utils.CheckUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BaiLan
 * @since 2022:05:24
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    private IAdminService adminService;
    private IStudentService studentService;

    @Autowired
    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }

    @Autowired
    public void setAdminService(IAdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping(consumes = "application/json")
    @ResponseBody
    public String changPassword(@RequestBody Map<String, String> map) {

        Admin user = CheckUser.getInstance(adminService);
        user.setId(map.get("id"));
        user.setPassword(map.get("password"));
        Admin check = CheckUser.check(adminService, user);
        if (check != null) {
            check.setPassword(map.get("newPassword"));
            adminService.updateById(check);
            return "succeed";
        }

        return "false";
    }


    @GetMapping(consumes = "application/json")
    @ResponseBody
    public Admin getInfo(@RequestBody Admin admin) {
        LambdaQueryWrapper<Admin> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper
                .in(Admin::getId, admin.getId());
        Admin one = adminService.getOne(LambdaQueryWrapper);
        System.out.println(one);
        if (Objects.nonNull(one)) {
            return one;
        } else {
            return null;
        }
    }

    @GetMapping(value = "/people")
    @ResponseBody
    public List<PeopleManagerDAO> getPeopleInfo() {
        return adminService.getStudent();
    }

    @PostMapping("/people")
    public boolean addPeople(@RequestBody Student student) {
        student.setPassword(student.getId());
        System.out.println(student);


        return studentService.save(student);
    }


    @DeleteMapping("/people")
    public boolean delPeople(@RequestBody Student student) {
        return studentService.removeById(student);
    }

    @PutMapping("/people")
    public boolean changePeople(@RequestBody Student student){
        return studentService.saveOrUpdate(student);
    }
}
